<?

include_once "accesscontrol.php";
include_once "index.php";

$from = "From: $_SERVER[HTTP_HOST] <$aset[ContactEmail]>";

if(isset($submit))
{
	if(empty($who))
	{
		echo "<center><br><br><br><span class=TNA>You miss to choose the emails recipient.<br> Go <a class=TNA href=mail.php> back </a> and make your choice.</span></center>";
		include_once('../footer.php');
		exit;
	}
	if(empty($message))
	{
		echo "<center><br><br><br><span class=TNA>You are trying to send blank email. <br> Go <a class=TNA href=mail.php> back </a> and write some text.</span></center>";
		include_once('../footer.php');
		exit;
	}
     if($_POST[who] == 'jobseekers')
     {
		$q1 = "select job_seeker_email from job_seeker_info ";
		$r1 = mysql_query($q1) or die(mysql_error());
		while($a1 = mysql_fetch_array($r1))
		 {
			mail($a1[0], $subject, $message, $from);
		 }
     }
     elseif($_POST[who] == 'employers')
     {
		$q1 = "select CompanyEmail from job_employer_info ";
		$r1 = mysql_query($q1) or die(mysql_error());
		while($a1 = mysql_fetch_array($r1))
		 {
			mail($a1[0], $subject, $message, $from);
		 }
     }
	 elseif($_POST[who] == "all")
	{
		$q1 = "select job_seeker_email from job_seeker_info ";
		$r1 = mysql_query($q1) or die(mysql_error());
		
		while($a1 = mysql_fetch_array($r1))		
		{
			$MyEmails[] = $a1[0];
		}

		$q2 = "select CompanyEmail from job_employer_info ";
		$r2 = mysql_query($q2) or die(mysql_error());
		while($a2 = mysql_fetch_array($r2))		
		{
			$MyEmails[] = $a2[0];
		}

		if(!empty($MyEmails))
		{
			while(list(,$v) = each($MyEmails))
			{
				mail($v, $subject, $message, $from);
			}

		}
	}



	echo "<center><br><br><br><span class=BlackLink>The mails was send successfully.</span></center>";
	unset($who);
	include_once('../footer.php');
	exit;
}
?>


<br><br><br>
<form action="<?=$PHP_SELF?>" method=post>
<table align="center" width="400" class="BlackText">
  <tr>
    <td colspan="2" align="center" class="HeaderClass"><font size="3"><b>Sending Mails </b></font><br />
        <br /></td>
  </tr>
</table>
<table align=center class=BlackLink>
<tr>
	<td align=left><b> Send emails to all: </b></td>
	<td>
	<select name=who>
		<option value="">Select </option>
		<option value="all">All</option>
		<option value="jobseekers">Jobseekers </option>
		<option value="employers">Employers </option>
	</select>
	</td>
</tr>

<tr>
	<td align=left><b>Subject: </b></td>
	<td><input type=text name=subject></td>
</tr>

<tr>
	<td valign=top align=left><b>Message:</b> </td>
	<td><textarea rows=15 cols=45 name=message></textarea></td>
</tr>

<tr>
	<td colspan=2 align=right>
	<input type=submit name=submit value=Send>	</td>
</tr>
</table>
</form>
<? include_once('../footer.php'); ?>